/*
 * Copyright (c) 2015. For Intelligent Group.
 */
package com.intelligent.ispc.client.core.view.analysis.grr.render;

import com.intelligent.ispc.client.core.modal.grr.SummaryTableModel;
import com.intelligent.ispc.client.utils.Colur;
import com.intelligent.ispc.client.utils.UIConstant;
import com.intelligent.ispc.constant.AppConstant;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;

/**
 * Created by cherry on 16/9/14.
 */
public class SummaryRender extends DefaultTableCellRenderer {

    private SummaryTableModel model;

    /**
     * summary render constructor
     *
     * @param table selected table
     */
    public SummaryRender(JTable table) {
        model = (SummaryTableModel) table.getModel();
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        String resultName = AppConstant.GRR_SUMMARY_TB_TITLE[7];
        JComponent component = (JComponent) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

        if (resultName.equals(table.getColumnName(column))) {
            String result = table.getValueAt(row, column).toString();
            setBackColor(result);
        } else {
            if (table.getSelectedRow() == row && column != table.getColumnCount() - 1) {
                component.setBackground(Colur.DARKBLUE);
                setForeground(Colur.WHITE);
            } else {
                setForeground(Colur.BLACK);
                setBackground(Colur.WHITE);
            }

        }
        setHorizontalAlignment(CENTER);
        return component;
    }

    private void setBackColor(String result) {
        String[] rules = UIConstant.ANALYSIS_GRR_RULES;
        if (rules[0].equals(result)) {
            setBackground(Colur.GREEN);
        } else if (rules[1].equals(result)) {
            setBackground(Colur.DEGREEN);
        } else if (rules[2].equals(result)) {
            setBackground(Colur.YELLOW);
        } else if (rules[3].equals(result)) {
            setBackground(Colur.RED);
        } else {
            setBackground(Colur.WHITE);
        }
    }

}
